package com.mayisoftware.mayioa.business.statistic.impl.controller;

import com.mayisoftware.mayioa.business.statistic.api.domain.*;
import com.mayisoftware.mayioa.business.statistic.api.service.IStatisticService;
import com.mayisoftware.mayioa.common.impl.utils.StringUtils;
import com.mayisoftware.mayioa.platform.api.domain.SysDept;
import com.mayisoftware.mayioa.platform.api.domain.SysDictData;
import com.mayisoftware.mayioa.platform.api.service.ISysDeptService;
import com.mayisoftware.mayioa.platform.api.service.ISysDictDataService;
import com.mayisoftware.mayioa.platform.impl.web.base.BaseController;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 骨干统计控制器
 */
@Controller
@RequestMapping("/statistic/overview")
public class StatisticOVController extends BaseController{

    /**
     * 统计服务
     */
    @Autowired
    private IStatisticService statisticService;
    /**
     * 部门服务
     */
    @Autowired
    private ISysDeptService sysDeptService;

    /**
     * 字典查询服务
     */
    @Autowired
    private ISysDictDataService sysDictDataService;
    /**
     * 前缀
     */
    private String prefix = "statistic/overview";

    /**
     * 统计页面
     */
    @RequiresPermissions("statistic:overview:view")
    @GetMapping()
    public String list()
    {
        return prefix + "/overview";
    }

    /**
     * 根据部门统计
     */
    @RequiresPermissions("statistic:overview:list")
    @PostMapping("/list/dept")
    @ResponseBody
    public EchartsPieVO listByDept()
    {

        EchartsPieVO echartsPieVO = new EchartsPieVO();

        List<StatisDept> statisDepts = statisticService.queryStatisByDept();

        if (statisDepts == null){
            return  echartsPieVO;
        }
        List<SysDept> sysDepts = sysDeptService.selectDeptList(new SysDept());
        Map<Integer,String> dept_name = new HashMap<>();

        for (SysDept dept:sysDepts ) {
            dept_name.put(dept.getDeptId().intValue(),dept.getDeptName());
        }

        List<String> deptNames = new ArrayList<>();
        List<SerizeData> serizeDatas = new ArrayList<>();
        for (StatisDept statisDept:statisDepts){
            if(StringUtils.isEmpty(statisDept.getDeptId())){
                continue;
            }
            String name =null;
            try{
                name=  dept_name.get(Integer.parseInt(statisDept.getDeptId()));

                if (name == null){
                    continue;
                }
            }
            //不是数字异常
            catch (Exception e){
                continue;
            }

            deptNames.add(name);

            SerizeData data = new SerizeData();
            data.setName(name);
            data.setValue(statisDept.getNum());
            serizeDatas.add(data);
        }

        echartsPieVO.setData(deptNames);
        echartsPieVO.setSerizeData(serizeDatas);
        return echartsPieVO;
    }

    /**
     * 根据骨干类型统计
     */
    @RequiresPermissions("statistic:overview:list")
    @PostMapping("/list/type")
    @ResponseBody
    public EchartsPieVO listByType()
    {
        EchartsPieVO res = new EchartsPieVO();

        List<StatisType> statisTypes = statisticService.queryStatisByType();

        if (statisTypes == null){
            return res;
        }

        //查询计划详细类型
        List<SysDictData> dictData = sysDictDataService.selectDictDataByType(StaticsConstants.DICT_PLAN_DETAIL_TYPE);

        Map<String,String> planTypeVal = new HashMap<>();

        for (SysDictData data:dictData) {
            planTypeVal.put(data.getDictValue(),data.getDictLabel());
        }

        List<String> data = new ArrayList<>();
        List<SerizeData> serizeDatas= new ArrayList<>();

        //读取数据详细信息
        for (StatisType statisType:statisTypes) {
            String statisValue = planTypeVal.get(statisType.getPlanType());

            if(statisValue == null){
                continue;
            }
            data.add(statisValue);
            SerizeData tmpData= new SerizeData();
            tmpData.setName(statisValue);
            tmpData.setValue(statisType.getNum());
            serizeDatas.add(tmpData);
        }

        //设置结果
        res.setSerizeData(serizeDatas);
        res.setData(data);
        return res;
    }

    /**
     * 根据日期统计
     */
    @RequiresPermissions("statistic:overview:list")
    @PostMapping("/list/date")
    @ResponseBody
    public EChartsLineVo listByDate()
    {
        EChartsLineVo res = new EChartsLineVo();
        List<StatisDate> statisDates = statisticService.queryStatisByDate();

        if (statisDates == null){
            return  res;
        }

        res.setText(StaticsConstants.STATIC_BONE_TIME_TITLE);
        List<String> data= new ArrayList<>();
        data.add(StaticsConstants.STATIC_BONE_TIME_TITLE_ITEM);
        res.setData(data);

        List<String> xAxisData = new ArrayList<>();
        List<SeriseData> seriseData = new ArrayList<>();

        res.setxAxisData(xAxisData);
        res.setSeriseData(seriseData);

        //设置数据
        List<Integer> nums = new ArrayList<>();
        for (StatisDate dateInfo : statisDates){
            xAxisData.add(dateInfo.getDateInfo());
            nums.add(dateInfo.getNum());
        }

        //配置x轴数据
        SeriseData tmpData = new SeriseData();
        tmpData.setName(StaticsConstants.STATIC_BONE_TIME_TITLE_ITEM);
        tmpData.setType(StaticsConstants.STATIC_BONE_TIME_CHARTTYPE);
        tmpData.setData(nums);
        seriseData.add(tmpData);
        return res;
    }



    /**
     * 根据Bg统计饼图
     */
    @RequiresPermissions("statistic:overview:list")
    @PostMapping("/list/listByBgPie")
    @ResponseBody
    public EchartsPieVO listByBg()
    {
        EchartsPieVO res = new EchartsPieVO();
        List<StatisDept> list= statisticService.countStatisByDeptType("BG");
        List<String> data = new ArrayList<String>();
        List<SerizeData> serizeData = new ArrayList<SerizeData>();
        res.setData(data);
        res.setSerizeData(serizeData);
        for(StatisDept sd: list){
            data.add(sd.getDeptName());
            SerizeData sdata = new SerizeData();
            sdata.setName(sd.getDeptName());
            sdata.setValue(sd.getNum());
            serizeData.add(sdata);
        }
        return res;
    }

    /**
     * 根据Bg统计柱状图
     */
    @RequiresPermissions("statistic:overview:list")
    @PostMapping("/list/listByBgBar")
    @ResponseBody
    public Map listByBgBar()
    {
        Map<String,Object> res = new HashMap<String,Object>();
        List<StatisDept> list= statisticService.countStatisByDeptType("BG");
        List<String> data = new ArrayList<String>();
        List<Integer> seriesData = new ArrayList<Integer>();
        for(StatisDept sd: list){
            data.add(sd.getDeptName());
            seriesData.add(sd.getNum());
        }
        res.put("legnedData",data);
        res.put("seriesData",seriesData);
        return res;
    }

}
